#include <bits/stdc++.h>
using namespace std;
const int N = 20, INF = 0x3f3f3f3f;

int n;
int a[N], t[N];

int check(int a, int b)
{
	int ans = 0;
	for(int i = 0;i < n;i ++)
	{
		if(((a >> i) & 1) == 0 && ((b >> i) & 1) == 1) ans ++;
		if(((a >> i) & 1) == 1 && ((b >> i) & 1) == 0) return -1;
	}
	return ans;
}

int solve()
{
	int ans = INF;
	// 枚举第一行所有的状态
	for(int st = 0;st < (1 << n);st ++)
	{
		memcpy(t, a, sizeof a);
		int change = st, cnt = 0;
		bool flag = true;
		for(int i = 1;i <= n;i ++)
		{
			// 判断 t[i]->change 是否合法
			int b = check(t[i], change);
			if(b == -1)
			{
				flag = false;
				break;
			}
			t[i] = change;
			cnt += b;
			change = t[i - 1] ^ (t[i] << 1) ^ (t[i] >> 1);
			change &= (1 << n) - 1;
		}
		if(flag) ans = min(ans, cnt);
	}
	return ans == INF ? -1 : ans;
}

int main()
{
	int T; cin >> T;
	for(int k = 1;k <= T;k ++)
	{
		memset(a, 0, sizeof a);
		cin >> n;
		for(int i = 1;i <= n;i ++)
		{
			for(int j = n - 1;j >= 0;j --)
			{
				int x; cin >> x;
				if(x == 1) a[i] |= (1 << j);
			}
		}
		printf("Case %d: %d\n", k, solve());
	}
	return 0;
}